package com.mySampleApplication.server.database;

import com.mySampleApplication.client.User;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

/**
 * Created by Navuhodonosor on 1/6/2015.
 */
public class UserService {
    public EntityManager manager = Persistence.createEntityManagerFactory("COT").createEntityManager();

    public User add(User user){
        manager.getTransaction().begin();
        User userFromDB = manager.merge(user);
        manager.getTransaction().commit();
        return userFromDB;
    }

    public void delete(String login){
        manager.getTransaction().begin();
        manager.remove(get(login));
        manager.getTransaction().commit();
    }

    private User get(String login) {
        return manager.find(User.class,login);
    }

    public void update(User user){
        manager.getTransaction().begin();
        manager.merge(user);
        manager.getTransaction().commit();
    }

    /*public List<User> getAll(){
        TypedQuery<User> namedQuery = manager.createNamedQuery("User.getAll", User.class);
        return namedQuery.getResultList();
    }*/

    public static void main(String[] arg){
        UserService userService = new UserService();
        userService.add(new User("Ivan","Bordov","Gamer","123456"));
    }
}
